package models

import (
	"database/sql"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

var db *sql.DB

func init() {
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/cmdb?charset=utf8mb4&loc=PRC&parseTime=true",
		beego.AppConfig.DefaultString("mysql::USERNAME", "root"),
		beego.AppConfig.DefaultString("mysql::PASSWORD", "root"),
		beego.AppConfig.DefaultString("mysql::HOST", "127.0.0.1"),
		beego.AppConfig.DefaultInt("mysql::PORT", 3306),
	)
	orm.RegisterDriver("mysql", orm.DRMySQL)
	orm.RegisterDataBase("default", "mysql", dsn) //注册数据库
	orm.RegisterModel(&User{})                    //注册模型
	orm.RunCommand()                              //RunCommand运行
	orm.RunSyncdb("default", false, true)
}
